package com.stylefeng.guns.modular.wehcat.controller;

import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.small.util.ResponseResultDTO;
import com.stylefeng.guns.modular.system.model.ArtificerMeans;
import com.stylefeng.guns.modular.system.service.IArtificerMeansService;
import com.stylefeng.guns.modular.wehcat.util.HttpClientUtils;
import com.stylefeng.guns.modular.wehcat.util.Sign;
import com.stylefeng.guns.modular.wehcat.util.WX_Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/wechat/public")
public class WechatLoginController extends BaseController {

    private static String PREFIX = "/wx/";

    @Resource
    private WX_Util wx_util;

    @Resource
    private IArtificerMeansService artificerMeansService;

    /**
     * 点击商家Tab重定向到回调地址获取code获取OpenID
     *
     * @param request
     * @return
     */
    @GetMapping("/toLogin")
    public String getOpenId(HttpServletRequest request) {
        return "wxlogin.html";
    }

    /**
     * 跳转登录页面
     *
     * @return
     */
    @GetMapping("/toLoginPage")
    private String toLoginPage() {
        return "wxlogin.html";
    }

    /**
     * 跳转我的页面
     *
     * @return
     */
    @GetMapping("/toMinePage")
    private String toMinePage() {
        return PREFIX + "wechat/my_frm.html";
    }

    /**
     * 跳转周边服务页面
     *
     * @return
     */
    @GetMapping("/toPeripheryPage")
    private String toPeripheryPage() {
        return PREFIX + "wechat/peripheryPage.html";
    }

    /**
     * 跳转技师列表
     *
     * @return
     */
    @GetMapping("/toArtificerListPage")
    private String toArtificerListPage() {
        return PREFIX + "wechat/artificerListPage.html";
    }

    /**
     * 跳转订单页面
     *
     * @return
     */
    @GetMapping("/toOrderPage")
    private String toOrderPage() {
        return PREFIX + "wechat/order_frm.html";
    }

    /**
     * 跳转资料编辑页面
     *
     * @return
     */
    @GetMapping("/editInfo")
    private String editInfo() {
        return PREFIX + "register.html";
    }


    /**
     * 配置微信公众号基本url，使其获取到token
     */
    @ResponseBody
    @RequestMapping("/check_url")
    public void weixininter(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //微信获取解析信息
        String str = wx_util.check_Url(request);
        PrintWriter out = response.getWriter();
        //输出
        out.print(str);
        //刷新
        out.flush();
        //关闭流
        out.close();
    }

    /**
     * 跳转优惠券页面
     *
     * @return
     */
    @GetMapping("/toCoupon")
    private String toCoupon() {
        return PREFIX + "wechat/couponPage.html";
    }

    /**
     * 跳转收藏页面
     *
     * @return
     */
    @GetMapping("/toCollection")
    private String toCollection() {
        return PREFIX + "wechat/collectionPage.html";
    }

    /**
     * 跳转通知页面
     *
     * @return
     */
    @GetMapping("/toNotice")
    private String toNotice() {
        return PREFIX + "wechat/noticePage.html";
    }

    /**
     * 跳转问题和反馈页面
     *
     * @return
     */
    @GetMapping("/toQuestion")
    private String toQuestion() {
        return PREFIX + "wechat/questionPage.html";
    }

    /**
     * 跳转反馈页面
     *
     * @return
     */
    @GetMapping("/toSayQuestion")
    private ModelAndView toSayQuestion(Integer id) {
        ModelAndView model = new ModelAndView();
        model.addObject("id", id);
        model.setViewName(PREFIX + "wechat/sayQuestionPage.html");
        return model;
    }

    /**
     * 百度转换位置
     *
     * @return
     */
    @RequestMapping("/getLocation")
    @ResponseBody
    public ResponseResultDTO getLocation(String lat, String lng) {
        ResponseResultDTO responseResultDTO = new ResponseResultDTO();
        try {
            if (StringUtils.isBlank(lat) || StringUtils.isBlank(lng)) {
                responseResultDTO.setStatus("-1");
                responseResultDTO.setMsg("PARAM_NULL");
                return responseResultDTO;
            }
            String url = "http://api.map.baidu.com/geoconv/v1/?coords=" + lng.trim() + "," + lat.trim() + "&from=1&to=5&ak=b5Kjd0zcpNax99AmviHxR2tXKIMGDnDA";
            JSONObject result = HttpClientUtils.httpGet(url);
            responseResultDTO.setData(result);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取地理位置失败");
            responseResultDTO.setStatus("-1");
            responseResultDTO.setMsg("获取地理位置失败");
        }
        return responseResultDTO;
    }
    /**
     * 获取地理位置签名
     *
     * @return
     */
    @RequestMapping("/getSign")
    @ResponseBody
    public ResponseResultDTO getSign(String url) {
        ResponseResultDTO responseResultDTO = new ResponseResultDTO();
        try {
            Map<String ,String>  map=  Sign.getSign(url);
            responseResultDTO.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取地理位置签名失败");
            responseResultDTO.setStatus("-1");
            responseResultDTO.setMsg("获取地理位置签名失败");
        }
        return responseResultDTO;
    }

    /**
     * 跳转到预约页面
     *
     * @return
     */
    @GetMapping("/toReservePage")
    private String toReservePage() {
        return PREFIX + "wechat/reservePage.html";
    }


    /**
     * 跳转到预约详情页面
     *
     * @return
     */
    @GetMapping("/toDetailedInfoPage")
    private String toDetailedInfoPage() {
        return PREFIX + "wechat/detailedInfoPage.html";
    }

    /**
     * 搜索的周边位置
     *
     * @return
     */
    @RequestMapping("/getAways")
    @ResponseBody
    public ResponseResultDTO getAways(String location,String  area) {
        ResponseResultDTO responseResultDTO = new ResponseResultDTO();
        try {
            if(StringUtils.isBlank(location)||StringUtils.isBlank(area)){
                responseResultDTO.setStatus("-1");
                responseResultDTO.setMsg("PARAM_IS_NULL");
                return responseResultDTO;
            }
            String url = "http://api.map.baidu.com/place/v2/suggestion?query="+location.trim()+"&region="+area.trim()+"&city_limit=true&output=json&ak=b5Kjd0zcpNax99AmviHxR2tXKIMGDnDA";
            JSONObject result = HttpClientUtils.httpGet(url);
            responseResultDTO.setData(result);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("搜索的周边位置失败");
            responseResultDTO.setStatus("-1");
            responseResultDTO.setMsg("搜索的周边位置失败");
        }
        return responseResultDTO;
    }


    /**
     * 周边服务带条件查询
     *
     * @return
     */
    @RequestMapping(value = "/searchByparam",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResultDTO searchByparam(@RequestParam("lat")String lat,@RequestParam("lng")String  lng,@RequestParam("isOnline")String  isOnline,
                                           @RequestParam("sex")String sex
            ,@RequestParam(defaultValue = "10",value = "distance")Integer distance) {
        ResponseResultDTO responseResultDTO = new ResponseResultDTO();
        try {
            List<ArtificerMeans>  list=artificerMeansService.searchByparam(lat,lng,sex,isOnline,distance);
            responseResultDTO.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("周边服务带条件查询失败");
            responseResultDTO.setStatus("-1");
            responseResultDTO.setMsg("周边服务带条件查询失败");
        }
        return responseResultDTO;
    }

    /**
     * 获取地理位置的经纬度
     *
     * @return
     */
    @RequestMapping(value = "/getLatAndLng",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResultDTO getLatAndLng(String address) {
        ResponseResultDTO responseResultDTO = new ResponseResultDTO();
        try {
            if(StringUtils.isBlank(address)){
                responseResultDTO.setMsg("PARAM_IS_NULL");
                responseResultDTO.setStatus("-1");
                return responseResultDTO;
            }

            String url = "http://api.map.baidu.com/geocoding/v3/?address="+address.trim()+"&output=json&ak=b5Kjd0zcpNax99AmviHxR2tXKIMGDnDA&callback=showLocation";
            String result = HttpClientUtils.httpGetStr(url);
            result = result.substring(result.indexOf("(")+1,result.lastIndexOf(")"));
            responseResultDTO.setData(JSONObject.parseObject(result));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取地理位置的经纬度失败");
            responseResultDTO.setStatus("-1");
            responseResultDTO.setMsg("获取地理位置的经纬度失败");
        }
        return responseResultDTO;
    }

}
